package com.example.xianji.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.example.xianji.widgets.CustomProgressDialog;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class Main_Want_ZiXunActivity extends Activity implements OnClickListener {
    private ImageView want_zixun_back;
    //    private SharedPreferences sp;
//    private Editor ed;
    private EditText edittext_zixun_liuyan;
    private Button junming_sumbit;
    private CustomProgressDialog mCustomProgressDialog;

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                if (mCustomProgressDialog != null) {
                    mCustomProgressDialog.dismiss();
                    mCustomProgressDialog = null;
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__want__zi_xun);
        //XJApplication.getInstance().addActivity(this);
        want_zixun_back = (ImageView) findViewById(R.id.want_zixun_back);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        edittext_zixun_liuyan = (EditText) findViewById(R.id.edittext_zixun_liuyan);
        junming_sumbit = (Button) findViewById(R.id.junming_sumbit);
        want_zixun_back.setOnClickListener(this);
        junming_sumbit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.want_zixun_back) {
            this.finish();
        } else if (v.getId() == R.id.junming_sumbit) {
            if (edittext_zixun_liuyan.getText().toString().length() > 0) {
                mCustomProgressDialog = CustomProgressDialog.createDialog(this);
                mCustomProgressDialog.setCanceledOnTouchOutside(false);
                mCustomProgressDialog.show();
                if (getIntent().getIntExtra("from_what", 0) == 1) {
                    sumbit_liuyan();
                } else if (getIntent().getIntExtra("from_what", 0) == 2) {
                    sumbit_liuyan_skill();
                }
            } else {
                Toast.makeText(Main_Want_ZiXunActivity.this, "请填写留言内容", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void sumbit_liuyan() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
            parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        } else {
            Toast.makeText(Main_Want_ZiXunActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(Main_Want_ZiXunActivity.this, Main_LoginActivity.class));
        }
        parms.addBodyParameter("details", edittext_zixun_liuyan.getText().toString());
        parms.addBodyParameter("convert_id", getIntent().getIntExtra("junming_id", 0) + "");
        httpUtils.send(HttpMethod.POST, Contant.junming_liuyan, parms, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                try {
                    JSONObject object = new JSONObject(arg0.result);
                    Toast.makeText(Main_Want_ZiXunActivity.this, object.getString("msg"), Toast.LENGTH_SHORT).show();
                    if (object.getInt("status") == 1) {
                        Main_Want_ZiXunActivity.this.finish();
                    }
                    handler.sendEmptyMessage(1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void sumbit_liuyan_skill() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
            parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        } else {
            Toast.makeText(Main_Want_ZiXunActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(Main_Want_ZiXunActivity.this, Main_LoginActivity.class));
        }
        parms.addBodyParameter("details", edittext_zixun_liuyan.getText().toString());
        parms.addBodyParameter("achieve_id", getIntent().getIntExtra("skill_id", 0) + "");
        httpUtils.send(HttpMethod.POST, Contant.skill_zixun, parms, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                try {
                    JSONObject object = new JSONObject(arg0.result);
                    Toast.makeText(Main_Want_ZiXunActivity.this, object.getString("msg"), Toast.LENGTH_SHORT).show();
                    if (object.getInt("status") == 1) {
                        Main_Want_ZiXunActivity.this.finish();
                    }
                    handler.sendEmptyMessage(1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}